﻿using System;
using System.Collections.Generic;

namespace GUI.BR
{
    using DAL.Entities;
    using BLL;
    using EXT;
    public partial class frmTratienPhieunhap : GUI.Danhmuc.ChitietDanhmuc.frmInputChitietDanhmuc
    {
        /// <summary>
        /// Truyền qua bao gồm Id_Phieunhap
        /// </summary>
        Guid Id_Phieunhap;

        public frmTratienPhieunhap(Guid Id_Phieunhap)
        {
            this.Id_Phieunhap = Id_Phieunhap;
            InitializeComponent();
        }

        TratienPhieunhapBLL TratienPhieunhap_BLL = new TratienPhieunhapBLL();
        PhieunhapBLL Phieunhap_BLL = new PhieunhapBLL();

        private void Load_Thongtinphieunhap(Guid Id_Phieunhap)
        {
            var dt = Phieunhap_BLL.PhieunhapLayThongtin1(Id_Phieunhap);
            if (dt.Rows.Count>0)
            {
                txtTongtien.EditValue = dt.Rows[0]["Tongtien"].ToInt32();
                txtDatra.EditValue = dt.Rows[0]["Datra"].ToInt32();
                txtConno.EditValue = dt.Rows[0]["Conno"].ToInt32();
                var dt2 = TratienPhieunhap_BLL.TratienPhieunhap_Select_By_Id_Phieunhap(Id_Phieunhap);
                if (dt2.Rows.Count>0)
                {
                    gcTratienPhieunhap.DataSource = dt2;
                }
            }
        }

        private void frmTratienPhieunhap_Load(object sender, EventArgs e)
        {
            Load_Thongtinphieunhap(Id_Phieunhap);
        }

        public event MyEvent_LoadData MyEvent_LoadData_Load_DSPhieu_Thongtinphie = null;

        private void btnTratien_Click(object sender, EventArgs e)
        {
            if (txtTrathem.EditValue.ToInt32()>txtConno.EditValue.ToInt32())
            {
                MsgC.MsgWarning("Chỉ còn nợ "+txtConno.Text);
                txtTrathem.Focus();
                return;
            }
            if (txtTrathem.EditValue.ToInt32()==0)
            {
                MsgC.MsgWarning("Số tiền trả phải lớn hơn không");
                txtTrathem.Focus();
                return;
            }
            Phieunhap_BLL._sqlMapper.BeginTransaction();

            try
            {

                TratienPhieunhap ttpn = new TratienPhieunhap();
                ttpn.Id = Guid.NewGuid();
                ttpn.Id_Phieunhap = Id_Phieunhap;
                ttpn.Ngay = DateTime.Now;
                ttpn.Sotien = txtTrathem.EditValue.ToInt32();
                ttpn.Id_Nhanvien = MAIN.Id;
                if (meoGhichu.Text.Trim() != "")
                {
                    ttpn.Ghichu = meoGhichu.Text.ToUpperFirst();
                }

                if (!TratienPhieunhap_BLL.TratienPhieunhap_Insert(ttpn))
                {
                    Phieunhap_BLL._sqlMapper.RollBackTransaction();
                    return;
                }

                DAL.Entities.Phieunhap pn = new Phieunhap();
                pn.Id = Id_Phieunhap;
                pn.Tongtien = txtTongtien.EditValue.ToInt32();
                pn.Datra = txtDatra.EditValue.ToInt32() + txtTrathem.EditValue.ToInt32();
                pn.Conno = pn.Tongtien - pn.Datra;
                if (txtTrathem.EditValue.ToInt32() == txtConno.EditValue.ToInt32())
                {
                    pn.Id_Trangthaiphieu=new Guid("a864d689-7c31-4bf4-b009-0405cfadef96");
                }

                if (txtTrathem.EditValue.ToInt32() < txtConno.EditValue.ToInt32())
                {
                    pn.Id_Trangthaiphieu= new Guid("a864d689-7c31-4bf4-b009-0405cfadef97");
                }
                if (Phieunhap_BLL.Phieunhap_CapnhatTratien(pn))
                {
                    Phieunhap_BLL._sqlMapper.CommitTransaction();
                    Load_Thongtinphieunhap(Id_Phieunhap);
                    txtTrathem.EditValue = null;
                    MsgC.MsgOK("Trả tiền thành công");
                    if (MyEvent_LoadData_Load_DSPhieu_Thongtinphie != null)
                    {
                        MyEvent_LoadData_Load_DSPhieu_Thongtinphie(this, new ELoadData() { IsLoadData=true});
                    }
                }
                else
                {
                    Phieunhap_BLL._sqlMapper.RollBackTransaction();
                    MsgC.MsgWarning("Không thêm tiền trả được\n\n Kiểm tra lại thông tin hoặc kết nối Server");
                }
            }
            catch (Exception)
            {
                Phieunhap_BLL._sqlMapper.RollBackTransaction();
                MsgC.MsgWarning("Không thêm tiền trả được\n\n Kiểm tra lại thông tin hoặc kết nối Server");
            }
        }
    }
}
